package handler

import (
	"github.com/gin-gonic/gin"
	"math/rand"
	"regexp"
	"schools/api-getaway/api/register"
	"schools/api-getaway/inits/cache"
	"schools/api-getaway/pkg"
	"strconv"
	"time"
)

func SendSms(r *gin.Context) {
	var so register.SendSms
	err := r.ShouldBind(&so)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "参数错误",
			Data:    nil,
		})
		return
	}

	if regexp.MustCompile(`^1[3-9]\d{9}$`).MatchString(so.Mobile) {
		r.JSON(400, &Res{
			Code:    400,
			Message: "手机号格式错误",
			Data:    nil,
		})
		return
	}

	code := rand.Intn(9000) + 1000
	codeKey := "sendSms" + so.Mobile
	pkg.SendSms(strconv.Itoa(code), so.Mobile)
	cache.Rdb.Set(cache.Ctx, codeKey, code, time.Minute*3)
	r.JSON(200, &Res{
		Code:    200,
		Message: "验证码发送成功",
		Data:    nil,
	})

}
